package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.controller.common.UserMsg;
import com.controller.reqdto.AddUserParam;
import com.controller.reqdto.LoginParam;
import com.controller.reqdto.UpdateUserParam;
import com.controller.respdto.UserLoginResp;
import com.controller.respdto.UserMsgList;
import com.controller.respdto.permission.auth.AuthParam;
import com.enums.CommonEnum;
import com.repository.mapper.AuthConfigMapper;
import com.repository.mapper.UserMapper;
import com.repository.po.AuthConfig;
import com.repository.po.User;
import com.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ruiwen
 * @since 2024-10-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    AuthConfigMapper authConfigMapper;

    @Override
    public int login(LoginParam loginParam) {
        List<User> users = baseMapper.selectList(new QueryWrapper<User>().eq("user_name", loginParam.getUserName()));
        if (users.size() < 1) {
            return 0;
        } else {
            //取第一个用户
            User user = users.get(0);
            if (user.getPassword().equals(loginParam.getPassword())) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public boolean addUser(AddUserParam addUserParam) {
        User dbUser = baseMapper.selectOne(new QueryWrapper<User>().eq("user_name", addUserParam.getUserName()));
        if (dbUser != null) {
            throw new RuntimeException("该用户已经存在");
        }
        User user = new User();
        BeanUtils.copyProperties(addUserParam, user);
        int insert = baseMapper.insert(user);
        return insert > 0;
    }

    @Override
    public UserLoginResp getUserMessage(LoginParam loginParam) {
        UserLoginResp authGroupResp = new UserLoginResp();
        User dbUser = baseMapper.selectOne(new QueryWrapper<User>().eq("user_name", loginParam.getUserName()));
        if (Objects.isNull(dbUser)) {
            throw new RuntimeException("该用户不存在");
        }
        UserMsg userMsg = new UserMsg();
        BeanUtils.copyProperties(dbUser, userMsg);
        authGroupResp.setUserMsg(userMsg);


        List<AuthConfig> authConfigs = authConfigMapper.selectList(null);
        List<AuthParam> buttonList = new ArrayList<>();
        for (AuthConfig authConfig : authConfigs) {
            AuthParam authParam = new AuthParam();
            BeanUtils.copyProperties(authConfig, authParam);
            if (dbUser.getRole().equals(CommonEnum.ADMIN.getCode())) {
                authParam.setIsShow(CommonEnum.SHOW_BUTTON.getCode());
            }
            buttonList.add(authParam);
        }
        authGroupResp.setButtonList(buttonList);
        return authGroupResp;
    }

    @Override
    public UserMsgList getUserList() {

        List<User> users = baseMapper.selectList(new QueryWrapper<User>().eq("role", CommonEnum.USER.getCode()));
        List<UserMsg> userMsgList = new ArrayList<>();
        for (User user : users) {
            UserMsg userMsg = new UserMsg();
            BeanUtils.copyProperties(user, userMsg);
            userMsgList.add(userMsg);
        }
        return UserMsgList.builder()
                .userMsgList(userMsgList)
                .build();
    }

    @Override
    public Boolean updateUserMsg(UpdateUserParam updateUserParam) {
        if (Objects.isNull(updateUserParam.getId())) {
            throw new RuntimeException("updateUserParam id 不能为空");
        }
        User user =new User();
        BeanUtils.copyProperties(updateUserParam,user);
        user.setRole(CommonEnum.USER.getCode());
        return baseMapper.updateById(user)>0;
    }

}
